const db = wx.cloud.database();
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    index2: 0,
    diqu: ['请选择地区'],
    bumen_name: ['请选择工作地点'],
    xingbie: 0,
    xingbies: ['请选择性别', '男', '女'],
    gangwei: 0,
    gangweis: ['请选择岗位类别', '综合类', '计算机类', '法律类'],
    pass: 0,
    passs: ['是否通过考试', '已通过', '未通过'],
    name: '',
    fenshu: '',
    phone: 0,
    theList: '',
    page: 32
  },
  xingbiesChange: function (e) {
    console.log('xingbies发送选择改变，携带值为', e.detail.value)
    this.setData({
      xingbie: e.detail.value
    })
  },
  gangweisChange: function (e) {
    console.log('gangweis发送选择改变，携带值为', e.detail.value)
    this.setData({
      gangwei: e.detail.value
    })
  },
  passsChange: function (e) {
    console.log('passs发送选择改变，携带值为', e.detail.value)
    this.setData({
      pass: e.detail.value
    })
  },

  nameInput: function (e) {
    this.setData({
      name: e.detail.value
    })
    console.log(this.data.name)
  },
  fenshuInput: function (e) {
    this.setData({
      fenshu: e.detail.value
    })
    console.log(this.data.fenshu)
  },

  bindPickerChange: function (e) {
    let num = e.detail.value;
    let that = this;
    console.log('diqu发送选择改变，携带值为', that.data.diqu[num]);
    this.setData({
      index: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/shaifen/getlevel?actid=44472&callback=?', //接收url
      data: {
        level: '2',
        grfiled: 'area',
        grtext: that.data.diqu[num]
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        var list = [];
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].bumen_name)
        }
        that.setData({
          bumen_name: list
        });
      }
    })
  },
  bindPickerChange2: function (e) {
    let num2 = e.detail.value;
    let that = this;
    console.log('bumen_name发送选择改变，携带值为', that.data.bumen_name[num2]);
    this.setData({
      index2: e.detail.value
    })

  },



  onLoad: function (options) {
    var that = this;
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/shaifen/getlevel?actid=44472&callback=?', //接收url
      data: {
        level: '1',
        area: ''
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        var list = [];
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].area)
        }
        that.setData({
          diqu: list
        });
      }
    })

    console.log(options);
    that.setData({
      area: options.area,
      geneal: options.geneal
    })
    if (options.page) {
      that.setData({
        page: options.page
      })
    }
  },



  getPhoneNumber(e) {
    var that = this;
    var diqu = that.data.diqu[that.data.index];
    var bumen_name = that.data.bumen_name[that.data.index2];
    var pass = that.data.passs[that.data.pass];
    var xingbie = that.data.xingbies[that.data.xingbie];
    var leibie = that.data.gangweis[that.data.gangwei];
    var xian = that.data.name;
    var fenshu = that.data.fenshu;

    wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
      wx.setStorageSync('phone', res.result)
      console.log(res.result)

      wx.request({
        url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
        method: 'POST',
        data: {
            activeId: 23,
            mobile: res.result,
            areaCode: that.data.area,
            agent: that.data.geneal,
            pageId: that.data.page
        },
        success: res => {
            console.log(res);
        }
    })

      wx.request({ //发送请求
        url: 'https://zg99.offcn.com/index/shaifen/register?actid=44472&callback=?', //接收url
        data: {
          diqu: diqu,
          bumen_name: bumen_name,
          xian: xian,
          pass: pass,
          leibie: leibie,
          xingbie: xingbie,
          fenshu: fenshu,
          phone: res.result,
          fenxiao: that.data.area,
          geneal: that.data.geneal,
        }, //接收参数
        success(res) {
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          }
          console.log(theJson)
          wx.navigateTo({
            url: '../list/list',
          })
        }
      })
    }).catch(err => {
      console.error(err);
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShdiquppMessage: function () {

  }
})